//topic.js

var app = getApp()
Page({
  data: {
    topic:'',
    reply: [],
    reply_length: 0,
    like:[],
    jsonresult: '',
    sourceUrl: 'https://eis.natappvip.cc/ITSupport',
    topicId:'',
    user:{},
    flag:false,
    replyContent:'',
    message: ''
  },

  onLoad: function (options) {
    this.setData({
      topicId:options.topicId,
      user: app.globalData.user
    })
    console.log("topicId:" + this.data.topicId)
    this.getTopic();
    this.getReply();
  },

  //网络请求topic数据
  getTopic: function () {
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/topic_getTopicById.action',
      data: {
          "topic.topicId":this.data.topicId
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          // that.setData({
          //   message: "页面空空如也~"
          // })
        } else {
          that.setData({
            topic: jsonresult.topic,
          })
          console.log("success")
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })
  },

  answerInput:function(e){
    this.setData({
      replyContent: e.detail.value
    })
  },

  inputfocus: function (e) {
    this.setData({
      message: ""
    })
  },

  //网络请求topic数据
  getReply: function () {
    var that = this
    wx.request({
      url: 'https://eis.natappvip.cc/ITSupport/reply_getAllReplyByTopicId.action',
      data: {
        "topic.topicId": this.data.topicId
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        let jsonresult = JSON.parse(res.data)
        console.log(jsonresult.result)
        if (jsonresult.result == "error") {
          // that.setData({
          //   message: "页面空空如也~"
          // })
        } else {
          that.setData({
            reply: jsonresult.reply,
            reply_length:jsonresult.reply.length
          })
          that.getLike(that.data.reply,that.data.reply_length)
          console.log("success")
        }
      },
      fail: function (res) {
        console.log("failed:" & res)
      }
    })
    
  },
  
  //根据reply创建like数据
  getLike: function (reply, reply_length){
    var like = new Array(this.data.reply_length)
    for (var i = 0; i < this.data.reply_length; i++) {
      like[i] = '../../image/unlike.png'
      if (reply[i].likes.length != 0){
        for (var j = 0; j < reply[i].likes.length; j++){
          if (reply[i].likes[j].user.userId == this.data.user.userId) {
            like[i] = '../../image/like.png'
          }
        }
      }
    }
    this.setData({
      like: like
    })
  },

  clickLike:function(e){
    var pid = e.currentTarget.dataset.pid
    var likecount = e.currentTarget.dataset.likecount + 1
    var replyId = e.currentTarget.dataset.replyId
    var likestr = "like[" + pid + "]"
    var replystr = "reply[" + pid + "].likeCount"
    var that = this
    if (this.data.like[pid] == '../../image/unlike.png'){
      wx.request({
        url: 'https://eis.natappvip.cc/ITSupport/reply_updateReplyLike.action',
        data: {
          "reply.replyId": replyId,
          "user.userId": this.data.user.userId
        },
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          let jsonresult = JSON.parse(res.data)
          // console.log(jsonresult.result)
          if (jsonresult.result == "error") {
            // that.setData({
            //   message: "页面空空如也~"
            // })
          } else {
            that.setData({
              [likestr]: "../../image/like.png",
              [replystr]: likecount
            })
            wx.showToast({
              title: '点赞成功',
              icon: 'success',
              duration: 2000
            })
            console.log("clickLike success")
          }
        }
     })
    }
    else{
      wx.showToast({
        title: '点过赞了~',
        icon: 'success',
        duration: 1000
      })
    }
  },

  replyQue:function(){
     this.setData({
       flag:true
     })
  },

  confirm:function(){
    var that = this
    if(this.data.replyContent != '') {
      wx.request({
        url: 'https://eis.natappvip.cc/ITSupport/reply_addReply.action',
        data: {
          "reply.topic.topicId": this.data.topicId,
          "reply.replyContent": this.data.replyContent,
          "reply.user.userId": this.data.user.userId
        },
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          let jsonresult = JSON.parse(res.data)
          if (jsonresult.result == "error") {

          } else {
            that.setData({
              flag: false,
              replyContent: ''
            })
            wx.showToast({
              title: '回答成功',
              icon: 'success',
              duration: 2000
            })
            that.getReply();
            that.setData({
              'topic.replyCount': that.data.topic.replyCount +1
            })
            console.log("reply success")
          }
        }
      })
    }
    else{
      that.setData({
        message: "还没有留下答案哦~"
      })
    }
  },

  cancel:function(){
    this.setData({
      flag: false,
      replyContent: ''
    })
  }

})
